#include "wxsguifactory.h"
#include "wxsgui.h"

#include <logmanager.h>
#include <wx/choicdlg.h>

wxsGUIFactory::wxsGUIFactory( const wxString& Name ): m_Name( Name ) {
  GetHash()[m_Name] = this;
}

wxsGUIFactory::~wxsGUIFactory() {
}

wxsGUI* wxsGUIFactory::Build( const wxString& Name, wxsProject* Project ) {
  if( GetHash().find( Name ) == GetHash().end() ) {
    return 0;
  }
  wxsGUIFactory* Factory = GetHash()[Name];
  wxsGUI* NewGUI = Factory->OnCreate( Project );
  if( NewGUI->GetName() != Name ) {
    Manager::Get()->GetLogManager()->DebugLog( _T( "wxSmith: Error while creating wxsGUI object (name mismatch)." ) );
    Manager::Get()->GetLogManager()->DebugLog( _T( "wxSmith:   Looks like bug in one wf wxsGUIFactory-derived classes or" ) );
    Manager::Get()->GetLogManager()->DebugLog( _T( "wxSmith:   some hack attempt." ) );
    delete NewGUI;
    return 0;
  }
  return NewGUI;
}

wxsGUI* wxsGUIFactory::SelectNew( const wxString& Message, wxsProject* Project ) {
  if( GetHash().empty() ) {
    return 0;
  }
  if( GetHash().size() == 1 ) {
    return Build( GetHash().begin()->first, Project );
  }
  wxArrayString GUIList;
  for( GUIItemHashT::iterator i = GetHash().begin(); i != GetHash().end(); ++i ) {
    GUIList.Add( i->first );
  }
  wxString SelectedGUI = ::wxGetSingleChoice( Message, _T( "Select GUI" ), GUIList );
  if( SelectedGUI.empty() ) {
    return 0;
  }
  return Build( SelectedGUI, Project );
}

inline wxsGUIFactory::GUIItemHashT& wxsGUIFactory::GetHash() {
  static GUIItemHashT Hash;
  return Hash;
}
